import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  GoodsType,
  GoodsRelevantItem,
  AreaList,
  GoodsHotItem
} from '@/types/data'
export default defineStore('goods', {
  state () {
    return {
      goodsInfo: {} as GoodsType,
      goodsRelevantList: [] as GoodsRelevantItem[],
      cityList: [] as AreaList[],
      initCityList: [] as AreaList[],
      goodsHotList: [[], [], []] as [
        GoodsHotItem[],
        GoodsHotItem[],
        GoodsHotItem[]
      ]
      // 元组  [number,number]  [1,2]
    }
  },
  actions: {
    async getGoods (id: string) {
      const res = await request.request<ApiRes<GoodsType>>({
        url: '/goods',
        params: {
          id
        }
      })
      this.goodsInfo = res.data.result
      console.log('商品详情', res)
    },
    // 同类商品推荐数据
    async goodsRelevant (id: string) {
      const res = await request.request<ApiRes<GoodsRelevantItem[]>>({
        url: '/goods/relevant',
        params: {
          id,
          limit: 16
        }
      })
      this.goodsRelevantList = res.data.result
      console.log('同类商品数据', res)
    },
    // 获取城市数据
    async getCityList () {
      // 基地址，如果你的地址不是http开头，它会拼接，如果是，不拼接
      const res = await request.request<AreaList[]>({
        url:
          'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      })
      this.cityList = res.data
      this.initCityList = res.data
      console.log('城市列表', res)
    },
    setCityList (item: AreaList[]) {
      this.cityList = item
    },
    // 还原城市数据
    resetCityList () {
      this.cityList = this.initCityList
    },
    // js数据是有大小限制
    async getGoodsHotList (id: string, type: number) {
      const res = await request.request<ApiRes<GoodsHotItem[]>>({
        url: '/goods/hot',
        params: {
          id,
          limit: 3,
          type: type
        }
      })
      this.goodsHotList[type - 1] = res.data.result
    }
  }
})
